package com.nike.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jfinal.plugin.activerecord.Page;
import com.nike.enums.AttributeType;
import com.nike.model.Productattribute;
import com.nike.model.Producttype;
import com.nike.router.RouterMapping;
import com.nike.utils.StringUtils;

/**
 * 商品属性类型
 * 
 * @author wuhantao
 *
 */
@RouterMapping(url = "/productAttribute", viewPath = "/view/productAttribute")
public class ProductAttributeController extends BaseController<Productattribute> {
	// 列表查询分页
	public void index() {
		String pageNumber = getPara("pageNumber");
		// 如果pageNumber为空，就默认为第1页
		if (!StringUtils.isNotBlank(pageNumber)) {
			pageNumber = "1";
		}

		Page<Productattribute> page = Productattribute.DAO.doPaginate(StringUtils.toInt(pageNumber, 1), 10);
		setAttr("page", page);
		render("list.html");
	}

	public void toAdd() {
		// 查询所有商品类型
		setAttr("allProductType", Producttype.DAO.doFind());
		// 获取所有商品属性类型
		setAttr("allAttributeType", getAllAttributeType());
		render("add.html");
	}

	// 获取所有商品属性类型
	public Map<String, String> getAllAttributeType() {
		Map<String, String> allAttributeType = new TreeMap<String, String>();
		for (AttributeType attributeType : AttributeType.values()) {
			allAttributeType.put(attributeType.getCode(), attributeType.getRemark());
		}
		return allAttributeType;
	}
	// 添加
	public void insert() throws JsonProcessingException {
		Productattribute productattribute = getModel(Productattribute.class, "productAttribute");
		// 属性类型
		AttributeType attributeType = AttributeType.valueOf(productattribute.getAttributeType());
		String[] attributeOptionList = getParaValues("attributeOptionList");
		if (attributeType == AttributeType.checkbox || attributeType == AttributeType.radio) {
			List<String> list = new ArrayList<String>();
			for (String optionList : attributeOptionList) {
				list.add(optionList);
			}
			ObjectMapper mapper = new ObjectMapper();
			// list转JSON
			String listforjson = mapper.writeValueAsString(list);
			productattribute.set("attributeOptionStore", listforjson);
			//productattribute.set("attributeOptionStore", JsonKit.toJson(list, 1));
		}
		productattribute.set("attributeTypeName", attributeType.getRemark());
		saved(productattribute);
		// 重定向
		redirect("/productAttribute");

	}
	// 删除
	public void delete() {
		String id = getPara("id");
		Productattribute.DAO.deleteById(id);
		redirect("/productAttribute");
	}
	// 编辑
	public void toEdit() throws IOException {
		String id = getPara("id");
		if (StringUtils.isNotBlank(id)) {
			Productattribute p = Productattribute.DAO.findById(id);
			ObjectMapper mapper = new ObjectMapper();
			// JSON转list
			List<String> stuList2 = mapper.readValue(p.getAttributeOptionStore(), List.class);
			setAttr("attributeOptionStoreList", stuList2);
			setAttr("productAttribute", Productattribute.DAO.findById(id));
		}
		// 查询所有商品类型
		setAttr("allProductType", Producttype.DAO.doFind());
		// 获取所有商品属性类型
		setAttr("allAttributeType", getAllAttributeType());
		render("update.html");

	}
	//修改
	public void update() throws JsonProcessingException {
		Productattribute productattribute = getModel(Productattribute.class, "productAttribute");
		AttributeType attributeType = AttributeType.valueOf(productattribute.getAttributeType());
		String[] attributeOptionList = getParaValues("attributeOptionList");
		if (attributeType == AttributeType.checkbox || attributeType == AttributeType.radio) {
			List<String> list = new ArrayList<String>();
			for (String optionList : attributeOptionList) {
				list.add(optionList);
			}
			ObjectMapper mapper = new ObjectMapper();
			// list转JSON
			String listforjson = mapper.writeValueAsString(list);
			productattribute.set("attributeOptionStore", listforjson);
		}
		productattribute.set("attributeTypeName", attributeType.getRemark());

		updated(productattribute);

		redirect("/productAttribute");
	}
	
	// 根据productTypeId获取已启用的商品属性JSON数据
		public void ajaxProductAttribute() {
			String productTypeId = getPara("productTypeId", "");
			if (StringUtils.isNotBlank(productTypeId)) {
				List<Productattribute> enabledProductAttributeList = Productattribute.DAO.getEnabledProductAttributeList(productTypeId);
				renderJson(enabledProductAttributeList);
			} else {
				renderJson("");
			}
		}
	

}
